package nachos.filesys;

import java.util.ArrayList;

import nachos.vm.VMProcess;

public class FileSysProcess extends VMProcess{

	public int handleSyscall(int syscall, int a0, int a1, int a2, int a3) {

		switch (syscall) {
			case syscallLs:
				System.out.println("");
				return handleLs(a0);
			case syscallTrans:
				return handleTrans(a0);
			case syscallCd:
				return handleCd(a0);
			case syscallTouch:
				return handleTouch(a0);
			case syscallRm:
				return handleRm(a0);
			case syscallRmdir:
				return handleRmdir(a0);
			case syscallMkdir:
				return handleMkdir(a0);
			default:
				return super.handleSyscall(syscall, a0, a1, a2, a3);
		}
	}
	private int handleMkdir(int vaddr) {
		// get the fileName
    	String fileName = readVirtualMemoryString(vaddr, maxLen);
		
		ArrayList<String> al = new ArrayList<String>();
    	al.add(fileName);
    	FileSysKernel.shMkdir(al);
		return 0;
	}
	private int handleRmdir(int vaddr) {
		// get the fileName
    	String fileName = readVirtualMemoryString(vaddr, maxLen);
		
		ArrayList<String> al = new ArrayList<String>();
    	al.add(fileName);
    	FileSysKernel.shRmdir(al);
    	
		return 0;
	}
	private int handleRm(int vaddr) {
		// get the fileName
    	String fileName = readVirtualMemoryString(vaddr, maxLen);
		
		ArrayList<String> al = new ArrayList<String>();
    	al.add(fileName);
    	FileSysKernel.shRm(al);
		return 0;
	}
	private int handleTouch(int vaddr) {
		// get the fileName
    	String fileName = readVirtualMemoryString(vaddr, maxLen);

    	ArrayList<String> al = new ArrayList<String>();
    	al.add(fileName);
    	FileSysKernel.shTouch(al);
    	
    	return 0;
	}
	private int handleCd(int vaddr) {
		// get the fileName
    	String fileName = readVirtualMemoryString(vaddr, maxLen);

    	ArrayList<String> al = new ArrayList<String>();
    	al.add(fileName);
    	FileSysKernel.shCd(al);
    	
    	return 0;
	}
	private int handleTrans(int vaddr) {
		// get the fileName
    	String fileName = readVirtualMemoryString(vaddr, maxLen);
    	
    	if (FileSysKernel.copy(fileName))
    		return 0;
    	
    	System.out.println("trans fail...");
		return 1;
	}
	private int handleLs(int option) {
		// get the option
    	String str = readVirtualMemoryString(option, maxLen);
		
		ArrayList<String> al = new ArrayList<String>();
		al.add(str);
		FileSysKernel.shLs(al);
		return 0;
	}
	private static final int
    syscallLs = 15,
    syscallTrans = 16,
    syscallCd = 17,
    syscallTouch = 18,
    syscallRm = 19,
    syscallRmdir = 20,
    syscallMkdir = 21;
	
	
	/**current absolute path of this process.*/
	public String pwd = null;
	
}
